package com.oba.king.businessprocess.storagemanage.outstorage;

/**
 * Title:           OutStorageBean
 * Description:
 * Copyright:       Copyright (c) 2010
 * Company:         King Top Limited
 * @author          Wu Liang
 * @version 1.0
 *
 * MODIFICATION HISTORY
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Date Modified        Modified By         Comments
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 24/05/2010           Wu Liang            Code Creation
 */
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.AbortProcessingException;
import javax.faces.model.SelectItem;

import com.icesoft.faces.component.paneltabset.TabChangeEvent;
import com.oba.base.dao.DBDataModel;
import com.oba.base.interfaces.IBaseBo;
import com.oba.base.interfaces.PageCallBack;
import com.oba.base.service.BaseBean;
import com.oba.king.businessprocess.storagemanage.instorage.InStorage;
import com.oba.king.businessprocess.storagemanage.instorage.InStorageCondition;
import com.oba.king.businessprocess.storagemanage.instorage.InStoragePrdt;
import com.oba.king.datalib.productinfo.ProductInfo;
import com.oba.king.sysmanager.Dept;

public class OutStorageBean extends BaseBean<OutStorage, OutStorageService> {

	private DBDataModel outStorageData = DBDataModel.create();
	
	private DBDataModel outStoragePrdtData = DBDataModel.create();
	
	private DBDataModel outStoragePrdtDetailData = DBDataModel.create();
	
	private OutStorageCondition outStorageCondition;
	private OutStorage outStorage;
	private OutStoragePrdt outStoragePrdt;
	private boolean auditForShow = false;
	private boolean showPrdtForOutStorage = false;//弹出层控制
	private final long STATUS_NEW = 1;
	private final long STATUS_PASSED = 2;
	private final long STATUS_REJECT = 3;
	private final long STATUS_MODIFY_NO_AUDIT = 4;
	private final String ADD_NEW_OUTSTORAGE_PATH = "/business/OutStorageAdd.jspx";
	private final String SHOW_INSTORAGE_PATH = "/business/OutStorageShow.jspx";
	private final String SHOW_AUDITING_OUTSTORAGE_PATH = "/business/OutStorageAudit.jspx";
	private final String SHOW_DETAIL_OUTSTORAGE_PATH = "/business/OutStorageDetailShow.jspx";
	private int focusIndex;//面板控制
	private List<SelectItem> queryDepartList;
	// 药品信息存放
	private DBDataModel choosePrdtData = DBDataModel.create();

	@Override
	public String doFind() {
		outStorageData.find(getService().getDao().buildAll());
		return null;
	}

	@Override
	public void init() {
		doFind();
		outStorageCondition = new OutStorageCondition();
		outStorage = new OutStorage();
		queryDepartList = new ArrayList<SelectItem>();
		queryDepartList.add(new SelectItem(null, "全部"));
		for (Dept dept : getService().getDao().buildAllDept()) {
			queryDepartList.add(new SelectItem(dept, dept.getName()));
		}
		auditForShow = false;
	}
	
	/**
	 * 点击出库单录入节点进入页面并初始化
	 */
	public void initAddOutStorage(){
		outStorage = new OutStorage();
		outStorage.setNewBo(true);
		outStoragePrdt = new OutStoragePrdt();
		queryDepartList = new ArrayList<SelectItem>();
		queryDepartList.add(new SelectItem(null, "全部"));
		for (Dept dept : getService().getDao().buildAllDept()) {
			queryDepartList.add(new SelectItem(dept, dept.getName()));
		}
		focusIndex = 0;
		goTarget(ADD_NEW_OUTSTORAGE_PATH);
	}
	
	/**
	 * 点击出库单审核节点进入页面进行初始化
	 */
	public void initAudit(){
		outStorageCondition = new OutStorageCondition();
		outStorageData.find(getService().getDao().findNeedAuditOutStorage());
		outStorage = new OutStorage();
		queryDepartList = new ArrayList<SelectItem>();
		queryDepartList.add(new SelectItem(null, "全部"));
		for (Dept dept : getService().getDao().buildAllDept()) {
			queryDepartList.add(new SelectItem(dept, dept.getName()));
		}
		auditForShow = true;
	}
	
	/**
	 *增加新的出库单:1跳转到增加页面 2初始化对象
	 */
	public void addNewOutStorageInfo(){
		focusIndex = 0;
		outStorage = new OutStorage();
		this.outStorage.setNewBo(true);
		this.goTarget(this.ADD_NEW_OUTSTORAGE_PATH);
	}
	
	/**
	 * 新增药品到出库中间表中：1弹出层显示 2初始化对象
	 */
	public void addNewPrdt2InStoragePrdt(){
		outStorageCondition = new OutStorageCondition();
		List<OutStoragePrdt> osplist = (List<OutStoragePrdt>) outStoragePrdtData.getWrappedData();
		List<Long> productIdList = new ArrayList<Long>();
		if(osplist != null && osplist.size()>0){
			for(int k = 0; k < osplist.size(); k++){
				productIdList.add(osplist.get(k).getProductInfo().getProductId());
			}
		}		
		choosePrdtData.find(this.getService().getDao().srchPrdtForChoose((List<Long>) productIdList));
		showPrdtForOutStorage = true;
	}
	
	/**
	 * 选择药品
	 */
	public void doChoosedPrdt(){
		// 选择这个药品
		if (choosePrdtData.isRowAvailable()) {
			ProductInfo prdtInfo = (ProductInfo) choosePrdtData.getRowData();
			List list = (List) outStoragePrdtData.getWrappedData();
			outStoragePrdt = new OutStoragePrdt();
			outStoragePrdt.setOutStorage(outStorage);
			outStoragePrdt.setProductInfo(prdtInfo);
			outStoragePrdt.setUnitPrice(prdtInfo.getProductUnitPrice());
			outStoragePrdt.setNewBo(true);
			outStoragePrdt.setProductInfo(prdtInfo);
			list.add(outStoragePrdt);
			System.out.println(""+list.size());
			showPrdtForOutStorage = false;
		}
	}
	
	/**
	 *  修改出库单
	 */
	public void doEditOutStorageInfo(){
		if (outStorageData.isRowAvailable()) {
			outStorage = (OutStorage) outStorageData.getRowData();
			focusIndex = 0;
			goTarget(ADD_NEW_OUTSTORAGE_PATH);
		}
	}
	
	/**
	 *删除出库单
	 */
	public void doDelOutStorageInfo(){
		if(outStorageData.isRowAvailable()){
			this.getService().getDao().deleteOutStorePrdt((OutStorage) outStorageData.getRowData());
			this.goTarget(this.SHOW_INSTORAGE_PATH);
			this.doFind();
		}		
	}
	
	/**
	 * 1保存到出库单 2跳转到增加入库产品面板
	 */
	public void doSaveOutStorageInfo(){
		String osp = new SimpleDateFormat("yyyyMMdd").format(new Date()) + outStorage.getBelegCode()+"O";
		outStorage.setCreateDate(new Date());
		outStorage.setOutDate(new Date());
		outStorage.setOutStorageCode(osp);
		outStorage.setUserInfo(getSysUser());
		if(outStorage.isNewBo()||"null".equals(outStorage.getAuditStatus().toString())){
			outStorage.setAuditStatus(STATUS_NEW);
		}else if("3".equals(outStorage.getAuditStatus().toString())){
			outStorage.setAuditStatus(STATUS_MODIFY_NO_AUDIT);
		}
		this.getService().save(this.outStorage);
		List<OutStoragePrdt> list = new ArrayList<OutStoragePrdt>();
		list = this.getService().getDao().srchOutStoragePrdt(outStorage);
		for (OutStoragePrdt i : list) {
			i.setChecked(true);
		}
		outStoragePrdtData.setWrappedData(list);
		focusIndex = 1;
	}
	
	/**
	 *  对出库单进行审核:1跳转到审核页面
	 */
	public void doAudit(){
		if(outStorageData.isRowAvailable()){
			outStorage = (OutStorage)outStorageData.getRowData();
			outStorageData.setWrappedData(getService().getDao().findOutStoragePrdtByOutStoId(outStorage));
			goTarget(SHOW_AUDITING_OUTSTORAGE_PATH);
		}
	}
	
	/**
	 * 用于审核不通过
	 */
	public void auditReject(){
		outStorage.setAuditStatus(STATUS_REJECT);
		getService().getDao().save(outStorage);
		outStorageData.find(getService().getDao().findNeedAuditOutStorage());
		this.auditForShow = true;
		goTarget(SHOW_INSTORAGE_PATH);
	}
	
	/**
	 * 用于审核通过
	 */
	public void auditPass(){
		outStorage.setAuditStatus(STATUS_PASSED);
		getService().getDao().save(outStorage);
		outStorageData.find(getService().getDao().findNeedAuditOutStorage());
		this.auditForShow = true;
		goTarget(SHOW_INSTORAGE_PATH);
	}
	
	/**
	 *保存入库中间表信息
	 */
	public void doSaveOutStoragePrdt(){
		if(outStoragePrdtData.isRowAvailable()){
			OutStoragePrdt osp = (OutStoragePrdt) outStoragePrdtData.getRowData();
			NumberFormat format = new DecimalFormat("#0.00");
			osp.setTotalSum(Double.valueOf(format.format(osp.getUnitPrice() * osp.getOutAmount())));
			osp.setChecked(true);
			getService().save(osp);
		}
	}
	
	/**
	 * 修改入库中间表信息 
	 */
	public void doEditOutStoragePrdt(){
		if (outStoragePrdtData.isRowAvailable()) {
			System.out.println("Entered the doEditInStoragePrdt to modify the inStoragePrdt");
			OutStoragePrdt osp = (OutStoragePrdt) outStoragePrdtData.getRowData();
			osp.setChecked(false);
		}
	}
	
	/**
	 * 删除入库中间表信息 
	 */
	public void doDelOutStoragePrdt(){
		if (outStoragePrdtData.isRowAvailable()) {
			outStoragePrdt = (OutStoragePrdt)outStoragePrdtData.getRowData();
			List list = (List)outStoragePrdtData.getWrappedData();
			list.remove(outStoragePrdt);
			getService().del(outStoragePrdt);			
		}
	}
	
	/**
	 * 出库单页面做的查询方法
	 */
	public void srchOutStorageCondition(){
		outStorageData.find(getService().getDao().srchByOutStorageCondition(outStorageCondition));
	}
	
	/**
	 * 弹出层中查询药品 
	 */
	public void srchByPrdtIdAndName(){
		List<OutStoragePrdt> list = (List) outStoragePrdtData.getWrappedData();
		List<Long> productIdList = new ArrayList<Long>() ;
		for(OutStoragePrdt osp: list){
			productIdList.add(osp.getProductInfo().getProductId());
		}
		choosePrdtData.find(this.getService().getDao().srchPrdtForChoose(productIdList, outStorageCondition));
	}
	
	/**
	 * 显示出库单中详细产品等信息
	 */
	public void doShowDetails(){
		if (outStorageData.isRowAvailable()) {
			outStorage = (OutStorage)outStorageData.getRowData();
			outStoragePrdtDetailData.find(this.getService().getDao().srchByOutStorageCode((OutStorage) outStorageData.getRowData()));
			goTarget(SHOW_DETAIL_OUTSTORAGE_PATH);
		}
	}
	
	/**
	 * 关闭弹出层
	 */
	public void closeAddPrdt(){
		this.showPrdtForOutStorage = false;
	}
	
	/**
	 * 返回面查询页面
	 */
	public void doRturnShow(){
		doFind();
		this.goTarget(this.SHOW_INSTORAGE_PATH);
	}
	
	/**
	 * 返回审核页面
	 */
	public void doRturnAuditShow(){
//		init();
//		auditForShow = true;
//		this.goTarget(this.SHOW_INSTORAGE_PATH);
	}
	
	/**
	 * 当点击同一页面不同面板时触发的事件
	 * @param tabChangeEvent
	 * @throws AbortProcessingException
	 */
	public void processTabChange(TabChangeEvent tabChangeEvent) throws AbortProcessingException {
		// 如果没有新增出库单，则不允许新增出库产品中间表
		if (outStorage.getId() == null) {
			focusIndex = 0;
		}else{
			//如果出库单存在并有数据，则需要显示修改及保存
			List<OutStoragePrdt> list = (List) outStoragePrdtData.getWrappedData();
			if(list != null) {
				for (OutStoragePrdt i : list) {
					i.setChecked(true);
				}
			}
		}
	}
	
	public void cleanButton(){
	    outStorageCondition = new OutStorageCondition();
	}
	
	public boolean isAuditForShow() {
		return auditForShow;
	}

	public void setAuditForShow(boolean auditForShow) {
		this.auditForShow = auditForShow;
	}

	public DBDataModel getOutStorageData() {
		return outStorageData;
	}

	public void setOutStorageData(DBDataModel outStorageData) {
		this.outStorageData = outStorageData;
	}

	public OutStorageCondition getOutStorageCondition() {
		return outStorageCondition;
	}

	public void setOutStorageCondition(OutStorageCondition outStorageCondition) {
		this.outStorageCondition = outStorageCondition;
	}
	
	public DBDataModel getOutStoragePrdtData() {
		return outStoragePrdtData;
	}

	public void setOutStoragePrdtData(DBDataModel outStoragePrdtData) {
		this.outStoragePrdtData = outStoragePrdtData;
	}

	public OutStorage getOutStorage() {
		return outStorage;
	}

	public void setOutStorage(OutStorage outStorage) {
		this.outStorage = outStorage;
	}

	public boolean isShowPrdtForOutStorage() {
		return showPrdtForOutStorage;
	}

	public void setShowPrdtForOutStorage(boolean showPrdtForOutStorage) {
		this.showPrdtForOutStorage = showPrdtForOutStorage;
	}

	public int getFocusIndex() {
		return focusIndex;
	}

	public void setFocusIndex(int focusIndex) {
		this.focusIndex = focusIndex;
	}
	
	public List<SelectItem> getQueryDepartList() {
		return queryDepartList;
	}

	public void setQueryDepartList(List<SelectItem> queryDepartList) {
		this.queryDepartList = queryDepartList;
	}

	public OutStoragePrdt getOutStoragePrdt() {
		return outStoragePrdt;
	}

	public void setOutStoragePrdt(OutStoragePrdt outStoragePrdt) {
		this.outStoragePrdt = outStoragePrdt;
	}

	public DBDataModel getChoosePrdtData() {
		return choosePrdtData;
	}

	public void setChoosePrdtData(DBDataModel choosePrdtData) {
		this.choosePrdtData = choosePrdtData;
	}

	public DBDataModel getOutStoragePrdtDetailData() {
		return outStoragePrdtDetailData;
	}

	public void setOutStoragePrdtDetailData(DBDataModel outStoragePrdtDetailData) {
		this.outStoragePrdtDetailData = outStoragePrdtDetailData;
	}

}
